package com.pratices.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;


//测试过滤192.168.101.91的请求
@WebFilter("/filter")
public class WhiteListFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String ip = servletRequest.getRemoteAddr();
        //设置字符集
        servletResponse.setContentType("application/json;charset=utf-8");
        if (ip.equals("192.168.101.91")) {
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            //在页面输出无法访问
            servletResponse.getWriter().write("无法访问");
        }
    }
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }
    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }
}
